package com.itchen.school.common.utils;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.google.common.collect.Lists;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Optional;
import java.util.TimeZone;
import org.apache.commons.lang3.time.DateUtils;

public class SysDateUtils extends DateUtils {

    public SysDateUtils() {}

    public static Date dateConverDate(Date date) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
            String s = sdf.format(date);
            return sdf.parse(s);
        } catch (Exception var3) {
            return date;
        }
    }

    public static Date getDateBeforeOrAfterDate(Date d, int day) {
        Calendar calendar = getCalendar();
        calendar.setTime(d);
        calendar.set(5, calendar.get(5) + day);
        return calendar.getTime();
    }

    public static Date dateConverDate(Date date, String format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
            String s = sdf.format(date);
            return sdf.parse(s);
        } catch (Exception var4) {
            return date;
        }
    }

    public static String toStr(Date date, String format) {
        return toStr(date, format, "GMT+8");
    }

    public static String toStr(Date date, String format, String timeZone) {
        if (date == null) {
            return null;
        } else {
            SimpleDateFormat simDateFormat = new SimpleDateFormat(format);
            if (StringUtils.isEmpty(timeZone)) {
                timeZone = "GMT+8";
            }

            simDateFormat.setTimeZone(TimeZone.getTimeZone(timeZone));
            return simDateFormat.format(date);
        }
    }

    public static String todayStr() {
        return toStr(new Date(), "yyyy-MM-dd");
    }

    public static String getProcessDurationInStr(Long num) {
        int day = 0;
        int hour = 0;
        int min = 0;
        if (null != num) {
            day = (int)(num / 86400000L);
            hour = (int)(num % 86400000L / 3600000L);
            min = (int)(num % 3600000L / 60000L);
        }

        return day + "天" + hour + "小时" + min + "分";
    }

    public static String dateConvertStr(Date date, String format) {
        return Optional.ofNullable(date).isPresent() ? toStr(date, format) : "";
    }

    public static String dateConvertStr(String strDate, String format) {
        if (StringUtils.isEmpty(strDate)) {
            return "";
        } else {
            try {
                long time = Long.valueOf(strDate);
                return toStr(new Date(time), format);
            } catch (Exception var4) {
                return "";
            }
        }
    }

    public static Date msStrToDate(String dateStr) {
        if (StringUtils.isEmpty(dateStr)) {
            return null;
        } else {
            try {
                long time = Long.valueOf(dateStr);
                return new Date(time);
            } catch (Exception var3) {
                return null;
            }
        }
    }

    public static String getDateBeforeOrAfter(Date d, int day, String format) {
        Calendar calendar = getCalendar();
        calendar.setTime(d);
        calendar.set(5, calendar.get(5) + day);
        return toStr(calendar.getTime(), format);
    }

    public static Date strConvertDate(String str) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));

        try {
            return sdf.parse(str);
        } catch (ParseException var3) {
            return null;
        }
    }

    public static Date strConvertDateFormat(String str, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));

        try {
            return sdf.parse(str);
        } catch (ParseException var4) {
            return null;
        }
    }

    public static Date strToDate(String str, String timeZone) {
        if (StringUtils.isEmpty(str)) {
            return null;
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            sdf.setTimeZone(TimeZone.getTimeZone(timeZone));

            try {
                return sdf.parse(str);
            } catch (ParseException var4) {
                return null;
            }
        }
    }

    public static String formatTimestamp(long longTime, String format) {
        if (StringUtils.isEmpty(format)) {
            format = "yyyy-MM-dd HH:mm:ss";
        }

        return toStr(new Date(longTime), format);
    }

    public static String formatTimestamp(long longTime, String format, String timeZone) {
        if (StringUtils.isEmpty(format)) {
            format = "yyyy-MM-dd HH:mm:ss";
        }

        return toStr(new Date(longTime), format, timeZone);
    }

    public static Date getBeginDayOfWeek() {
        Date date = new Date();
        Calendar cal = getCalendar();
        cal.setTime(date);
        int dayofweek = cal.get(7);
        if (dayofweek == 1) {
            dayofweek += 7;
        }

        cal.add(5, 2 - dayofweek);
        return getDayStartTime(cal.getTime());
    }

    public static Date getEndDayOfWeek() {
        Date date = new Date();
        Calendar cal = getCalendar();
        cal.setTime(date);
        int dayofweek = cal.get(7);
        if (dayofweek == 1) {
            dayofweek += 7;
        }

        cal.add(5, 8 - dayofweek);
        return getDayStartTime(cal.getTime());
    }

    public static Date getTimesMonthmorning() {
        Calendar cal = getCalendar();
        cal.set(cal.get(1), cal.get(2), cal.get(5), 0, 0, 0);
        cal.set(5, cal.getActualMinimum(5));
        return cal.getTime();
    }

    public static Date getMonthEndTime(Date timeStamp) {
        Calendar calendar = getCalendar();
        calendar.setTime(timeStamp);
        calendar.add(1, 0);
        calendar.add(2, 0);
        calendar.set(5, calendar.getActualMaximum(5));
        calendar.set(11, 0);
        calendar.set(12, 0);
        calendar.set(13, 0);
        calendar.set(14, 999);
        return calendar.getTime();
    }

    public static Integer getNowYear() {
        Date date = new Date();
        GregorianCalendar gc = (GregorianCalendar)getCalendar();
        gc.setTime(date);
        return gc.get(1);
    }

    public static Date getBeginDayOfYear() {
        Calendar cal = getCalendar();
        cal.set(1, getNowYear());
        cal.set(2, 0);
        cal.set(5, 1);
        return getDayStartTime(cal.getTime());
    }

    public static Date getDayStartTime(Date d) {
        Calendar calendar = getCalendar();
        if (null != d) {
            calendar.setTime(d);
        }

        calendar.set(calendar.get(1), calendar.get(2), calendar.get(5), 0, 0, 0);
        calendar.set(14, 0);
        return new Date(calendar.getTimeInMillis());
    }

    public static int getYear(Date date) {
        Calendar calendar = getCalendar();
        calendar.setTime(date);
        return calendar.get(1);
    }

    public static int getMonth(Date date) {
        Calendar calendar = getCalendar();
        calendar.setTime(date);
        return calendar.get(2);
    }

    public static int getMonth(Date date, int fix) {
        Calendar calendar = getCalendar();
        calendar.setTime(date);
        calendar.add(2, fix);
        return calendar.get(2);
    }

    public static Calendar getCalendar() {
        return getCalendar("GMT+8");
    }

    public static Calendar getCalendar(String s) {
        Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone(s));
        return c;
    }

    public static SimpleDateFormat getSimpleDateFormat(String format) {
        SimpleDateFormat simDateFormat = new SimpleDateFormat(format);
        simDateFormat.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        return simDateFormat;
    }

    public static Date getMonthBegin(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(5, 1);
        c.set(11, 0);
        c.set(12, 0);
        c.set(13, 0);
        c.set(14, 0);
        return c.getTime();
    }

    public static boolean isDateStr(String str, String format) {
        boolean convertSuccess = true;
        SimpleDateFormat f = new SimpleDateFormat(format);

        try {
            f.setLenient(false);
            f.parse(str);
        } catch (ParseException var5) {
            convertSuccess = false;
        }

        return convertSuccess;
    }

    public static int getWeekOfYear(Date date) {
        Calendar cal = Calendar.getInstance();
        if (null == date) {
            cal.setTime(new Date());
        } else {
            cal.setTime(date);
        }

        return cal.get(3);
    }

    public static int getCurWeekValue() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        return cal.get(7) - 1;
    }

    public static String getCurWeekCn() {
        String[] weekDays = new String[]{"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        int w = cal.get(7) - 1;
        if (w < 0) {
            w = 0;
        }

        return weekDays[w];
    }

    public static int getDays(Date date, int type) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.getActualMaximum(type);
    }

    public static List<String> getDays(Integer year, Integer month) {
        Calendar start = Calendar.getInstance();
        Calendar end = Calendar.getInstance();
        if (null == month) {
            start.set(year, 0, 1);
            end.set(year + 1, 0, 0);
        } else {
            start.set(year, month - 1, 1);
            end.set(year, month, 0);
        }

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        ArrayList result = Lists.newArrayList();

        while(start.compareTo(end) <= 0) {
            result.add(format.format(start.getTime()));
            start.set(5, start.get(5) + 1);
        }

        return result;
    }

    public static String getFirstDayOfMonth(int year, int month) {
        Calendar cal = Calendar.getInstance();
        cal.set(1, year);
        cal.set(2, month - 1);
        int firstDay = cal.getMinimum(5);
        cal.set(5, firstDay);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(cal.getTime());
    }

    public static String getLastDayOfMonth(int year, int month) {
        Calendar cal = Calendar.getInstance();
        cal.set(1, year);
        cal.set(2, month - 1);
        int lastDay = cal.getActualMaximum(5);
        cal.set(5, lastDay);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(cal.getTime());
    }

    public static long getToLong(String DateTime) throws Exception {
        return strConvertDateFormat(DateTime, "yyyy-MM").getTime();
    }

    public static long getToLong(String DateTime, String format) throws Exception {
        return strConvertDateFormat(DateTime, format).getTime();
    }

    public static void main(String[] args) {
        System.out.println(getMonth(new Date()));
    }

}

